package com.zmn.uac.api.controllers.oauth.login;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.uac.api.constant.HeaderParamKeyConst;
import com.zmn.uac.business.interfaces.oauth.login.KuaiShouLoginBService;
import com.zmn.uac.common.dict.ErrorCodeDict;
import com.zmn.uac.common.dio.oauth.login.UserAuthLoginDIO;
import com.zmn.uac.common.dro.oauth.login.UserAuthLoginDRO;
import com.zmn.uac.common.info.kuaishou.KuaiShouAuthPhoneInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 快手授权登陆
 *
 * @author Rananquan
 * @since 2022年5月16日 18点38分
 */
@RestController
@RequestMapping("/kuaishou")
@Slf4j
@Api(tags = "快手授权登陆")
public class KuaiShouLoginController extends LoginBaseController {
    private static final String TAG = "快手授权登陆";

    @Resource
    private KuaiShouLoginBService kuaiShouLoginBService;

    /**
     * <p>参考资料：
     * <ul>
     *     <li><a href="https://mp.kuaishou.com/docs/develop/guide/thirdPartyLogin.html">小程序登录流程</a>，登录需要做 Step7 ~ Step10 的功能</li>
     *     <li><a href="https://mp.kuaishou.com/docs/develop/server/code2Session.html">code2Session</a>，使用 js_code 置换 session_key 和 open_id</li>
     *     <li><a href="https://mp.kuaishou.com/docs/develop/functionAccessGuide/getUserPhoneNumber.html">获取手机号</a></li>
     * </ul>
     */
    @ApiOperation("小程序手机号授权登录")
    @PostMapping(value = "/login",produces = MediaType.APPLICATION_JSON_VALUE,
            headers = {
                    HeaderParamKeyConst.PLAT_ID_KEY,
                    HeaderParamKeyConst.CHANNEL_ID_KEY,
                    HeaderParamKeyConst.PROVINCE_ID_KEY,
                    HeaderParamKeyConst.CITY_ID_KEY,
                    HeaderParamKeyConst.PROVINCE_NAME_KEY,
                    HeaderParamKeyConst.CITY_NAME_KEY
    })
    public ResponseDTO<UserAuthLoginDRO> login(HttpServletRequest request,
                                               @RequestBody @Valid
                                               @NotNull(message = "快手用户授权手机号信息不能为空") KuaiShouAuthPhoneInfo authPhoneInfo) {
        UserAuthLoginDIO dio = getUserAuthLogin(request, authPhoneInfo.getCode(), authPhoneInfo.getEntrance());

        log.info("[{}], {}, 授权登陆请求参数: {}", TAG, dio.getUuid(), dio);
        log.info("[{}], {}, 授权手机号请求参数: {}", TAG, dio.getUuid(), authPhoneInfo);

        if (Objects.isNull(dio.getChannelId())) {
            return ResponseDTO.fail(ErrorCodeDict.ERROR_CODE_PARAM_NULL, "渠道ID不能为空!");
        }

        ResponseDTO<UserAuthLoginDRO> responseDTO = kuaiShouLoginBService.login(dio, authPhoneInfo);

        log.info("[{}], {}, 快手授权登陆调用响应: {}", TAG, dio.getUuid(), responseDTO);

        UserAuthLoginDRO dro = responseDTO.getData();
        if (Objects.nonNull(dro)) {
            log.info("[{}], {}, 快手授权登陆成功，{}", TAG, dio.getUuid(), dro);
            return ResponseDTO.success(dro);
        }

        log.info("[{}], {}, 快手授权登陆失败，{}", TAG, dio.getUuid(), responseDTO.getMessage());
        return ResponseDTO.fail(responseDTO.getMessage());
    }
}
